home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
-
- require 5;
-
- $bgcolor = "#f9f9f9";
- $doctype = "<!DOCTYPE HTML PUBLIC" .
- "\"-//W3C//DTD HTML 3.2//EN\">\n";
- $comment = "<!-- Generated by infosearch manTOC -->\n";
- $bHTML = "<HTML>\n";
- $eHTML = "</HTML>\n";
- $bTitle = "<HEAD>\n <TITLE>";
- $eTitle = "</TITLE>\n</HEAD>\n";
- $bBody = "<BODY BGCOLOR=\"$bgcolor\">";
- $eBody = "</BODY>\n";
- $cgiBase = "<A HREF=\"/cgi-bin/infosrch.cgi?cmd=getdoc&db=man&fname=";
- $cgiBrowseBase = "<A HREF=\"/cgi-bin/infosrch.cgi?cmd=browse&db=man&fname=";
- $cgiMan = " <A HREF=\"/cgi-bin/infosrch.cgi?cmd=browse&db=man\">";
- $bBQ = "<P>";
- $eBQ = "</P>";
-
- $makewhatis = "/usr/share/catman/whatis";
- $sgiIndex = "/usr/share/catman/SGIindex";
- $fmtwidth = 23;
- $tdebug = 0;
-
- %sectionCount = ();
-
- %sectionNames = (
- "0" => "Man Pages",
- "1" => "User Commands (1)",
- "2" => "System Calls (2)",
- "3" => "Subroutines (3)",
- "4" => "File Formats (4)",
- "5" => "Miscellaneous (5)",
- "6" => "Games and Demos (6)",
- "7" => "Special Files (7)",
- "8" => "System Administration (8)",
- "D" => "Device Driver (D)"
- );
-
- $arg1 = $ARGV[0];
-
- # If -whatis, then also output the man title data to the whatis database
-
- if($ARGV[1] eq "-whatis"){
- $whatis = 1;
- }else{
- $whatis = 0;
- }
-
- # if -coll then add "&coll=" to the getdoc URL
- if ($ARGV[1] eq "-coll" && $cgiBase =~ /(.*)&db=man(.*)/) {
- $cgiBase = $1 . "&coll=" . $ARGV[2] . "&db=man" . $2;
- }
-
- # if -coll then add "&coll=" to the browse URL
- if ($ARGV[1] eq "-coll" && $cgiBrowseBase =~ /(.*)&db=man(.*)/) {
- $cgiBrowseBase = $1 . "&coll=" . $ARGV[2] . "&db=man" . $2;
- }
-
- if ($arg1 eq ""){
- # Build from default title file location
- &buildFTIndexList("${sgiIndex}/man.ttl");
- }else{
- if($arg1=~/\.ttl/){
- # Build from a title file
- &buildFTIndexList($arg1,$whatis);
- }else{
- # Build from makewhatis database (called from CGI)
- &buildMakeWhatIndex($makewhatis,$arg1);
- }
- }
-
-
- #######################################################################
- #
- # buildFTIndexList(<index title file>)
- #
- #######################################################################
-
-
- sub buildFTIndexList{
-
- local($ttl) = shift @_;
- local($whatis) = shift @_;
- local($inBQ) = 0;
- local($lastAlpha) = "";
- local($thisAlpha) = "";
-
- if ( -r $ttl ){
- $titlefiles = $ttl;
- }else{
- die "Unable to read $ttl\n";
- }
-
- $tocPath = $ttl;
- $tocPath =~ s/(SGIindex)\/.+/$1/;
-
- $localttl = $ttl;
- $localttl =~ s/([^\/]*)\.ttl$/local$1\.ttl/;
-
- if ( -e $localttl ){ # Only if the local file exists (else strings error)
- $titlefiles = "$titlefiles $localttl";
- }
-
- open(TITLESTR, "strings $titlefiles |" )
- || die "Error: Can't open $ttl\n";
-
- while(<TITLESTR>){ # Build a list of all the files in the index
-
- if (/^F(.+)\n/){ # File Line
- # Take the filename exactly as it appears in the index
- # title file (.ttl file).
- $filename=$1;
- }elsif(/^t(.+)\n/){ # Title Line
- $title = $1;
- $filename=~s/\+/%2b/g; # Files with + in the name break the cgi
-
- if($title =~/\((\d)/){ # Get Section from title
- $section = $1;
- }elsif($filename =~/cat(\d)/){ # Guess Section from filename
- $section = $1;
-
- } elsif ($filename =~ /catD/) {
- $section = "D";
- }else{
- $section = 5;
- }
-
- if ($section ne "D" &&
- ($section > 8 || $section < 1)) { # Fix out of range sections
- $section=5; # Misc Section
- }
-
- push(@{$section}, "$title\t$filename");
- $sectionCount{$section}++;
- }
- }
- close(TITLESTR);
-
- # Remove existing index files
- system("rm -f $tocPath/cat*.html $tocPath/man.html");
- die "rm failed" if ($?);
-
- &initTOCFiles($tocPath);
-
- # Bug cat4.html /dev/fs is not correctly indented
- # if we start at $i=4, it works
-
- if($whatis){
- open(WHATIS, "|sort -f -u > $makewhatis")
- || die "Error: Can't open $makewhatis";
- }
-
- foreach $i (keys %tocFiles) {
-
- open($tocFiles{$i}, ">> $tocFiles{$i}")
- || die "Can't open section file\n";
-
- &HTMLFileHeader($tocFiles{$i}, $sectionNames{$i}, "$i");
- &alphaHeader($tocFiles{$i});
-
- @{$i} = sort sortCaseInsensitive @{$i};
-
- $inBQ=0;
- $lastAlpha = "";
- $thisAlpha = "";
-
- # Print each title to the correct file
-
- foreach (@{$i}){
-
- if(/([^\t]*)\t(.*)/){
- $title = $1;
- $whatistitle = $title;
- $filename = $2;
- $title=&formatTitle($title);
-
- $thisAlpha=&setAlpha($title);
- &alphaIndexIfNeeded($inBQ,$tocFiles{$i},$lastAlpha,$thisAlpha);
- $lastAlpha = $thisAlpha;
-
- if($inBQ == 0){ # Correct indentation
- $indent = $bBQ;
- $inBQ = 1;
- }else{
- $indent = "";
- }
-
- if($whatis){ # Also output to whatis man database
- # in the exact same format as makewhatis
- $j = 0;
- $k = 0;
-
- $whatistitle =~s/\ +$//; # Remove trailing spaces
-
- # Debug for man page titles that we can't read.
-
- if($whatistitle =~/\[No\ title\]/){
- if($tdebug == 1){
- $whatistitle = "[No title] $filename";
- }else{
- $whatistitle = ""; # Skip [No title]
- }
- }
-
- while(substr($whatistitle,$j,$j+1)){
-
- if((substr($whatistitle,$j,1) eq "-") && ($j > 0) &&
- (substr($whatistitle,$j-1,1) eq " ") &&
- ($j <= $fmtwidth)){
-
- $k = $fmtwidth-$j+1;
- while($k > 0){
- print WHATIS " "
- or die "print: $!";
- $k--;
- }
- print WHATIS substr($whatistitle,$j,1024)
- or die "print: $!";
- last;
- }
- print WHATIS substr($whatistitle,$j,1)
- or die "print: $!";
- $j++;
- }
- print WHATIS "\n"
- or die "print: $!";
- }
-
- print {$tocFiles{$i}} " ",
- "${cgiBase}${filename}\">${title}<BR>\n"
- or die "print: $!";
-
- }
-
- }
- if($inBQ == 1){
- print {$tocFiles{$i}} "$eBQ\n"
- or die "print: $!";
- $inBQ=0;
- }
- &HTMLFileTrailer($tocFiles{$i});
- }
-
-
- open($tocFiles{'0'}, "> $tocFiles{'0'}")
- || die "Can't open section file\n";
- &HTMLFileHeader($tocFiles{'0'}, $sectionNames{0}, '0');
- &outputTopIndex($tocFiles{'0'});
- &HTMLFileTrailer($tocFiles{'0'});
- close($tocFiles{'0'}) or die "close tocfile";
-
- $manmsg = "updated man page whatis database";
- system("logger -t sgindexAdmin \"$manmsg\"");
- die "logger failed" if ($?);
- }
-
- #######################################################################
- #
- # buildMakeWhatIndex(<makewhatis,sectionNumber>)
- #
- #######################################################################
-
- sub buildMakeWhatIndex{
-
- local($makewhat,$targetString) = @_;
- local($inBQ) = 0;
- local($lastAlpha) = "";
- local($thisAlpha) = "";
-
- if($targetString=~/cat([0-8])/){
- # Output index for this $targetSection only
- $targetSection = $1;
- }elsif($targetString=~/man/){
- $targetSection = 0; # Top level man index
- }else{
- die "invalid arg: $targetString - use: man or cat[1-8]";
- }
-
- open(TITLESTR,$makewhat) ||
- die "can't open $makewhat";
-
- &HTMLFileHeader(STDOUT,$sectionNames{$targetSection}, $targetSection);
-
- if($targetSection != 0){
- &alphaHeader(STDOUT);
- }
-
- while(<TITLESTR>){ # Build a list of all the files in the index
-
- if (/(\S+)\ *\((\d)[^)]{0,3}\)/){
-
- $cmd = $1;
- $section = $2;
- $title = $_;
-
- if($section > 8 || $section < 1){ # Correct bogus section numbers
- $section=5; # Misc Section
- }
- $sectionCount{$section}++;
-
- $title=&formatTitle($title);
- $thisAlpha=&setAlpha($title);
- if($targetSection != 0){
- &alphaIndexIfNeeded($inBQ,STDOUT,$lastAlpha,$thisAlpha);
- $lastAlpha = $thisAlpha;
- }
-
- if($section == $targetSection){
- if($inBQ == 0){
- $indent = $bBQ;
- $inBQ = 1;
- }else{
- $indent = "";
- }
- print "${indent}",
- "${cgiBase}${section}%20${cmd}\">${title}<BR>\n"
- or die "print: $!";
- }
- }
- }
-
- close(TITLESTR);
-
- if($targetSection == 0){
- &outputTopIndex(STDOUT);
- }
-
- if($inBQ == 1){
- print "$eBQ\n"
- or die "print: $!";
- }
-
- &HTMLFileTrailer(STDOUT);
-
- }
-
- #######################################################################
- #
- # outputTopIndex()
- #
- #######################################################################
-
- sub outputTopIndex{
-
- local($fd) = @_;
- local $icon =
- "<IMG SRC=\"/infosearch/images/folder.gif\" " .
- "BORDER=\"0\" ALIGN=\"BOTTOM\" ALT=\"+\">";
-
- # Print the browse top level browse index
-
- print ${fd} "<p>\n" or die "print: $!";
- foreach $i (sort keys %sectionCount) {
-
- if ($sectionCount{$i} != 0){
- $linkdoc = sprintf("cat%s.html",$i);
- print ${fd} " ",
- "$cgiBrowseBase$linkdoc\">$icon<\/A> ",
- "$cgiBrowseBase$linkdoc\">$sectionNames{$i}<\/A><BR>\n"
- or die "print: $!";
- }
- }
- print ${fd} "</p>\n" or die "print: $!";
- }
-
- #######################################################################
- #
- # replaceHTMLEntities(string)
- #
- #######################################################################
-
- sub replaceHTMLEntities{
-
- local($str) = @_;
-
- $str =~ s/</</g;
- $str =~ s/>/>/g;
-
- return $str;
-
- }
-
- #######################################################################
- #
- # HTMLFileHeader(fd, title, targetid)
- #
- #######################################################################
-
- sub HTMLFileHeader{
-
- local($filehandle,$title,$target)= @_;
-
- print { $filehandle }
- "$doctype$comment$bHTML$bTitle$title$eTitle$bBody\n"
- or die "print: $!";
-
- my($db_ttl) = $sectionNames{'0'};
-
- print { $filehandle }
- "<h3>", ($target ne '0' ? "${cgiMan}${db_ttl}</a> : " : ''),
- "${title}</H3>\n"
- or die "print: $!";
-
- }
-
-
- #######################################################################
- #
- # HTMLFileTrailer(fd)
- #
- #######################################################################
-
- sub HTMLFileTrailer{
-
- local($filehandle) = @_;
-
- print { $filehandle } "$eBody$eHTML"
- or die "print: $!";
-
- }
-
- #######################################################################
- #
- # initTOCFiles(tocPath)
- #
- #######################################################################
-
- sub initTOCFiles{
-
- local($tocPath) = @_;
-
- %tocFiles = (
- '0' => "$tocPath/man.html",
- '1' => "$tocPath/cat1.html",
- '2' => "$tocPath/cat2.html",
- '3' => "$tocPath/cat3.html",
- '4' => "$tocPath/cat4.html",
- '5' => "$tocPath/cat5.html",
- '6' => "$tocPath/cat6.html",
- '7' => "$tocPath/cat7.html",
- '8' => "$tocPath/cat8.html",
- 'D' => "$tocPath/catD.html");
-
- }
-
- #######################################################################
- #
- # formatTitle(titleString)
- #
- #######################################################################
-
- sub formatTitle{
-
- local($title) = @_;
-
- $title = &replaceHTMLEntities($title);
- $title =~ s/\ +/\ /g; # Remove duplicate spaces
- $title =~ s/\n//; # Remove any \n
- unless($title=~ s/\)/\)<\/A>/){ # Insert </A>
- unless($title=~s/-/<\/A>-/){
- $title=~s/$/<\/A>/;
- }
- }
- return $title;
-
- }
-
- #######################################################################
- #
- # setAlpha(titleString)
- #
- #######################################################################
-
- sub setAlpha{
-
- local($title) = @_;
- local($alpha) = "";
-
- if($title=~/^([a-zA-Z])/){
- $alpha=$1;
- $alpha=~tr/a-z/A-Z/;
- }
-
- return $alpha;
-
- }
-
-
- #######################################################################
- #
- # alphaHeader()
- #
- #######################################################################
-
- sub alphaHeader{
-
- local($filehandle) = @_;
-
- local(@alphabet) =
- (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);
-
- print {$filehandle} "<P><center><B>\n"
- or die "print: $!";
-
- foreach(@alphabet){
- print {$filehandle} "<A HREF=\"#$_\">$_</A> \n"
- or die "print: $!";
- }
-
- print {$filehandle} "</B></center></P>\n"
- or die "print: $!";
- }
-
- #######################################################################
- #
- # alphaIndex($inBQ,$fd,$lastAlpha,$thisAlpha)
- #
- #######################################################################
-
- sub alphaIndexIfNeeded{
-
- local($fd);
- ($inBQ, $fd, $lastAlpha, $thisAlpha) = @_;
-
- local(@alphabet) =
- (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);
-
- if($thisAlpha ne $lastAlpha && $thisAlpha ne ""){
- if($inBQ == 1){
- $exdent = "$eBQ\n";
- $inBQ = 0;
- }else{
- $exdent = "";
- }
-
- $off = (ord($thisAlpha)-ord($lastAlpha));
-
- print {$fd} $exdent;
-
- if($off > 1 && $lastAlpha ne ""){
- local($a) = 0;
- while($lastAlpha ne $alphabet[$a]){
- $a++;
- }
- $a = $a+1;
- for($j=0;$j<$off;$j++){
- print {$fd} "<H3><A NAME=\"$alphabet[$j+$a]\">" .
- "$alphabet[$j+$a]</A></H3>\n"
- or die "print: $!";
- }
- }else{
- print {$fd} "<H3><A NAME=\"$thisAlpha\">" .
- "$thisAlpha</A></H3>\n"
- or die "print: $!";
- }
- }
-
-
- $lastAlpha = $thisAlpha;
-
- }
-
- #######################################################################
- #
- # sortCaseInsensitive
- #
- #######################################################################
-
- sub sortCaseInsensitive
- {
- local($tmp_a) = $a;
- local($tmp_b) = $b;
-
- $tmp_a =~ tr/A-Z/a-z/;
- $tmp_b =~ tr/A-Z/a-z/;
-
- return($tmp_a cmp $tmp_b);
- }
-
-